package wt.hpad.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * Created by wt on 2017-7-20.
 */

public class IPUtil {

    /**
     * 将得到的int类型的IP转换为String类型
     *
     * @param ip
     * @return
     */
    private static String intIP2StringIP(int ip) {
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                (ip >> 24 & 0xFF);
    }

    /**
     * 获取手机ip地址
     *
     * @param context
     * @return "127.0.0.1" 表示获取失败
     */
    public static String getIPAddress(Context context) {
        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
                try {
                    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                        NetworkInterface intf = en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                                return inetAddress.getHostAddress().toString();
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ipAddress = IPUtil.intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
                return ipAddress;
            }
        } else {
            //当前无网络连接,请在设置中打开网络
        }
        return "127.0.0.1";
    }

    /**
     * ping 一个 ip 地址
     *
     * @param ip
     * @param timeOut
     * @return 这里有网络延时
     */
    public static boolean pingIP(String ip, int timeOut) {
        boolean status = false;
        try {
            status = InetAddress.getByName(ip).isReachable(timeOut);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return status;
        }
    }


    /**
     * pc 单网卡,获取ip地址
     *
     * @return
     */
    public static String getIP__PC_OneNetCard() {
        try {
            InetAddress myip = InetAddress.getLocalHost();
            return myip.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * pc 多网卡,获取ip地址
     * @return
     */
    public static ArrayList getIP__PC_MoreNetCard(String hostName) {
        ArrayList<String> ipArr = new ArrayList<String>();
        try {
            InetAddress[] myips = InetAddress.getAllByName(hostName);
            for (int i = 0; i < myips.length; i++) {
                ipArr.add(myips[i].getHostAddress());
            }
            return ipArr;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }


}
